﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace Agents.Admin
{
    //this page is for representing all information about an Agent's actions on a given date.
    public partial class AgentFile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // Add the printing script
            string printScript =
                @"function PrintGridView()
                 {
                    var w = 800, h = 500;
                    var left = (screen.width / 2) - (w / 2);
                    var top = (screen.height / 2) - (h / 2);
                    var gridInsideDiv = document.getElementById('gvDiv');
                    var pageURL = '';
                    var title = 'הדפסת יומן סוכן';
                    var printWindow = window.open(pageURL, title,
                        'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, ' +
                        'copyhistory=no, width=' + w +
                        ', height=' + h + ', top=' + top + ', left=' + left);
                    printWindow.document.write('<html dir=rtl><head><title>' + title + 
                                                '</title></head><body>');
                    printWindow.document.write(gridInsideDiv.innerHTML);
                    printWindow.document.write('</body></html>');
                    printWindow.document.close();
                    printWindow.focus();
                    printWindow.print();
                    printWindow.close();}";
            
            this.ClientScript.RegisterStartupScript(Page.GetType(), 
                        "PrintGridView", printScript.ToString(), true);
            printBtn.Attributes.Add("onclick", "PrintGridView();"); 
        }

        protected void backBtn_Click(object sender, EventArgs e)
        {
            string name = Request.QueryString["agentId"];
            //redirecting back to the administraters home page.
            Response.Redirect("ShowAgent.aspx?agentId=" + name);
        }

        /// <summary>
        /// Takes care of the page indexing of the DataGrid
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void dgLogActions_PageIndexChanged(object sender, DataGridPageChangedEventArgs e)
        {
            // Changing the data grid viewing to the selected index
            dgLogActions.CurrentPageIndex = e.NewPageIndex;

            // Binding the data grid data
            dgLogActions.DataBind();
        }

        /// <summary>
        /// Occurs after binding is complete, and hides the data grid if there are no results
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            if (e.AffectedRows == 0)
            {
                dgLogActions.Visible = false;
                lblMessage.Text = "אין יומן בתאריך זה";
            }
            else
            {
                lblMessage.Text = "";
            }
        }
    }
}